{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ONNX Script GELU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "from onnxscript import (\n",
    "    script, opset18 as op, FLOAT\n",
    ")\n",
    "\n",
    "M_SQRT1_2 = math.sqrt(0.5)\n",
    "\n",
    "@script()\n",
    "def gelu(X: FLOAT[...]):\n",
    "    phiX = 0.5 * (op.Erf(M_SQRT1_2 * X) + 1.0)\n",
    "    return X * phiX\n",
    "\n",
    "model = gelu.to_model_proto()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import onnx\n",
    "onnx.save_model(\n",
    "    model,\n",
    "    \"gelu.onnx\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xin",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
